using Protocol.Code;
using Protocol.Dto.Fight;
using Script.Framework;
using Script.Model;
using Script.Scene;
using Script.Scene.Msg;
using UnityEngine.UI;

namespace Script.UI._2.Fight
{
    /// <summary>
    /// 游戏结束面板
    /// </summary>
    public class OverPanel : UIBase
    {
        private Text textWinIdentity;
        private Text textWinBean;
        private Button btnBack;

        private void Awake()
        {
            Bind(UIEvent.SHOW_OVER_PANEL);
        }


        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.SHOW_OVER_PANEL:
                    refreshShow(message as OverDto);
                    break;
            }
        }


        private void Start()
        {
            textWinIdentity = transform.Find("txtWhoWin").GetComponent<Text>();
            textWinBean = transform.Find("txtBeans").GetComponent<Text>();
            btnBack = transform.Find("btnBack").GetComponent<Button>();


            btnBack.onClick.AddListener(backClick);


            SetPanelActive(false);
        }

        private void backClick()
        {
            LoadSceneMsg loadSceneMsg = new LoadSceneMsg(1,
                delegate
                {
                    SocketMsg socketMsg = new SocketMsg(OpCode.USER, UserCode.Get_INFO_CREQ, null);

                    Dispatch(AreaCode.NET, 0, socketMsg);
                });

            Dispatch(AreaCode.SCENE, SceneEvent.LOAD_SCENE, loadSceneMsg);
        }


        private void refreshShow(OverDto _overDto)
        {
            SetPanelActive(true);

            textWinBean.text = _overDto.BeanCount + "";
            textWinIdentity.text = _overDto.WinIdentity == 0 ? "农民" : "地主";

            if (_overDto.WinUidList.Contains(Models.GameModel.Id))
            {
                textWinIdentity.text += "胜利";
                textWinBean.text = "欢乐豆,+" + _overDto.BeanCount;
            }
            else
            {
                textWinIdentity.text += "失败";
                textWinBean.text = "欢乐豆,-" + _overDto.BeanCount;
            }
        }
    }
}